package com.keyinong.caizhengting.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DataUtil {
    /**
     * 格式化日期为String型：  yyyy-MM-dd HH:mm:ss
     *
     * @param inTime
     * @return
     * @author 阳熙
     */
    public static String getStringDateTime(Object inTime) {
        if (inTime instanceof String) {
            inTime = getDateTime((String) inTime);
        }
        SimpleDateFormat dfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String outTime = dfTime.format(inTime);
        return outTime;
    }

    /**
     * 格式化日期为String型： yyyy-MM-dd
     *
     * @param inTime
     * @return
     * @author 阳熙
     */
    public static String getStringDate(Object inTime) {
        if (inTime instanceof String) {
            inTime = getDateTime((String) inTime);
        }
        SimpleDateFormat dfTime = new SimpleDateFormat("yyyy-MM-dd");
        String outTime = dfTime.format(inTime);
        return outTime;
    }

    /**
     * 根据字符串得到yyyy-MM-dd HH:mm:ss格式日期
     *
     * @param dateString
     * @return
     */
    public static Date getDateTime(String dateString) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = df.parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据字符串得到yyyy-MM-dd格式日期
     *
     * @param dateString
     * @return
     */
    public static Date getDate(String dateString) {
        DateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = dd.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 得到指定日期的星期几
     *
     * @param date
     * @return
     */
    public static int getWeek(Date date) {
//        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
//        String week = sdf.format(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 得到指定日期是当月第几日
     *
     * @param date
     * @return
     * @author 阳熙
     */
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 将String日期转化为Long型，精确到毫秒值，如果转换失败，则为-1；
     *
     * @param stringDate
     * @return
     */
    public static Long getLongDate(String stringDate) {
        SimpleDateFormat dfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long longDate = -1L;
        try {
            longDate = dfTime.parse(stringDate).getTime();
        } catch (ParseException e) {
            System.out.println("转换日期格式出错！");
            e.printStackTrace();
        }
        return longDate;
    }

    /**
     * @param n
     * @return 当前日期的后n天
     */
    public static String afterNowDay(int n) {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        c.setTime(new Date());
        c.add(Calendar.DATE, n);
        Date d2 = c.getTime();
        String s = df.format(d2);
        return s;
    }

    /**
     * @param n
     * @return 当前日期的前n天
     */
    public static String beforNowDay(int n) {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        c.setTime(new Date());
        c.add(Calendar.DATE, -n);
        Date d2 = c.getTime();
        String s = df.format(d2);
        return s;
    }

    /**
     * 获得指定时间date前n天日期
     *
     * @param n
     * @return
     * @author 阳熙
     */
    public static String beforTheDay(String date, int n) {
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date tdate = null;
        try {
            tdate = df.parse(date);
        } catch (ParseException e) {
            System.out.println("异常信息：" + "转换时间异常！");
            e.printStackTrace();
        }
        c.setTime(tdate);
        c.add(Calendar.DATE, -n);
        Date d2 = c.getTime();
        String s = df.format(d2);
        return s;
    }

    /**
     * 得到与date日期相差time天的日期
     *
     * @param date 给出的日期
     * @param time 相差的天数，正的为date前time天的日期，负的为date后time天的日期
     * @return
     */
    public static Date getDateAfterDate(Date date, int time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, time);
        date = calendar.getTime();
        return date;
    }

    //当前日期加几天或者减几天
    public static Date addday(String Date, int day) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //当前日期加几天
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        //System.out.println("到期:"+cal.getTime().toLocaleString());
        return cal.getTime();
    }

    /**
     * 判断当前日期是星期几
     *
     * @param pTime 设置的需要判断的时间  //格式如2012-09-08
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */

    //  String pTime = "2012-03-12";
    public static String getWeek(String pTime) {


        String Week = "";


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {

            c.setTime(format.parse(pTime));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "六";
        }


        return Week;
    }


}
